#include "ClientMenuState.h"
#include "MainMenuState.h"
#include "ClientGamePlayState.h"
#include "ClientConnectNetworkEvent.h"
#include "ClientReadyToPlayNetworkEvent.h"
#include "UpdatePlayerNetworkEvent.h"
#include "Client.h"
#include "Configuration.h"
#include "Text.h"
#include "BackgroundImage.h"
#include "CountDownState.h"

ClientMenuState::ClientMenuState(ClientPtr client)
        : MenuState("Client Menu"), isReady(false), client(client)
{
}

ClientMenuState::~ClientMenuState()
{
}

void ClientMenuState::init()
{
    MenuState::init();
    ClientConnectNetworkEvent event((Configuration::get(Configuration::CLIENT_NAME)).c_str());
    client->send(event);
}

void ClientMenuState::addMenuLines()
{
    addLine("1. Ready to play");
    addLine("ESC. Back to main menu");
    addLine("");
}

GameState* ClientMenuState::addPlayerText(const std::string& playerName, const Color& color)
{
    Text* playerText = addColorLine(color, playerName);
    playerTexts.push_back(playerText);
    return this;
}

GameState* ClientMenuState::clearPlayerTexts()
{
    clearMenu();
    addMenuLines();
    return this;
}

GameState* ClientMenuState::performMenuActionEscape()
{
    client->quit();
    return this;
}

GameState* ClientMenuState::performMenuAction1()
{
    if (!isReady)
    {
        ClientReadyToPlayNetworkEvent event;
        client->send(event);
        isReady = true;
    }
    return this;
}

GameState* ClientMenuState::handleClientDisconnectedEvent()
{
    return goToNextState(new MainMenuState());
}

GameState* ClientMenuState::handleServerGameStartEvent(unsigned int widthOfWell)
{
    GameState* nextState = new ClientGamePlayState(client, widthOfWell);
    return goToNextState(new CountDownState(nextState));
}

GameState* ClientMenuState::handleEvent(const Event& event)
{
    event.visitClientMenuState(this);
    return MenuState::handleEvent(event);
}
